package cn.comon.app.scan.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * APP商店接入数据时，举报信息是否有效的判断过程中，定时的清楚缓存中的map
 * @author xujf
 *
 */
public class AppstoreDuplClear implements Runnable{
    private Map<String, Integer> cacheMap = null;
    private long time = 0;
    private int removeTime = 0;
    
    /**
     * 
     * @param cacheMap 需要清理的map
     * @param time 执行清理时间间隔（单位：毫秒）
     * @param removeTime map中信息对应的时间大于该值时，移除。（单位：秒）
     */
    public AppstoreDuplClear (Map<String, Integer> cacheMap, long time, int removeTime) {
        this.cacheMap = cacheMap;
        this.time = time;
        this.removeTime = removeTime;
    }
    public void run() {
        while (true) {
            try {
                Thread.sleep(this.time);
                Set<String> keysSet = cacheMap.keySet();
                Iterator<String> iterator = keysSet.iterator();
                while(iterator.hasNext()) {
                    Object key = iterator.next();//key
                    if (UtilTools.getTimestamp(0) - cacheMap.get(key).intValue() > removeTime) {
                        cacheMap.remove(key);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
